
layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin;
        var  add_level = false;
        var addUserIndex = null;

    initLevel();
    table.render({
        elem: '#agency_level_table'
        ,url: '/agency_level/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'id', title: 'id',width:100, "align":"center"}
            ,{field:'name',width:200, title: '等级名称',"align":"center"}
            ,{field:'level',width:200, title: '等级',"align":"center"}
            ,{field:'price',width:200, title: '等级价格(/元)',"align":"center",templet:function (d){
                return d.price==null?0:new Number(d.price.toString()).div(new Number(100));
            }}
            ,{field:'consumeProfit',width:200, title: '结算价(百分比)',"align":"center",templet:function (d){
                return d.consumeProfit==null?0:new Number(d.consumeProfit.toString()).mul(new Number(100));
            }}
            ,{field:'directConsumeProfit',width:200, title: '直推消费分佣(百分比)',"align":"center",templet:function (d){
                    return d.directConsumeProfit==null?0:new Number(d.directConsumeProfit.toString()).mul(new Number(100));
                }}
            ,{field:'indirectConsumeProfit',width:200, title: '间推消费分佣(百分比)',"align":"center",templet:function (d){
                    return d.indirectConsumeProfit==null?0:new Number(d.indirectConsumeProfit.toString()).mul(new Number(100));
                }}
            ,{field:'thirdConsumeProfit',width:200, title: '三级消费分佣(百分比)',"align":"center",templet:function (d){
                    return d.thirdConsumeProfit==null?0:new Number(d.thirdConsumeProfit.toString()).mul(new Number(100));
                }}
            ,{field:'subsidyProfit',width:200, title: '补贴(百分比)',"align":"center",templet:function (d){
                    return d.subsidyProfit==null?0:new Number(d.subsidyProfit.toString()).mul(new Number(100));
                }}

            ,{field:'directRecAgencyProfit',width:200, title: '直推代理商分佣(百分比)',"align":"center",templet:function (d){
                    return d.directRecAgencyProfit==null?0:new Number(d.directRecAgencyProfit.toString()).mul(new Number(100));
                }}
            ,{field:'indirectRecAgencyProfit',width:200, title: '间推代理商分佣(百分比)',"align":"center",templet:function (d){
                    return d.indirectRecAgencyProfit==null?0:new Number(d.indirectRecAgencyProfit.toString()).mul(new Number(100));
                }}

            ,{field:'createdTime',width:200, title: '创建时间',"align":"center"}

            ,{field:'OPT', width:100, title: '操作',"align":"center",templet:function (d) {
                    return     '<a  href="#" class="layui-btn layui-btn-xs" lay-event="modify_level" >编辑</a>';
                }}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{}
        ,id: 'agency_level_table'
        ,page: true
    });

    table.on('tool(agency_level_table)', function(obj){
        add_level = false;

        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_level_win")
            ,end:function () {
                table.reload('agency_level_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                    }
                });
            }
        });

        var table_event = obj.event;

        var data = obj.data;

        if(table_event=="modify_level"){
            form.val('add_level_form', {
                "id":data.id,
                "name":data.name,
                "level": data.level,
                "price": data.price==null?0:new Number(data.price.toString()).div(new Number(100)),
                "subsidyProfit": data.subsidyProfit==null?0:new Number(data.subsidyProfit.toString()).mul(new Number(100)),
                "consumeProfit": data.consumeProfit==null?0:new Number(data.consumeProfit.toString()).mul(new Number(100)),
                "directConsumeProfit": data.directConsumeProfit==null?0:new Number(data.directConsumeProfit.toString()).mul(new Number(100)),
                "indirectConsumeProfit": data.indirectConsumeProfit==null?0:new Number(data.indirectConsumeProfit.toString()).mul(new Number(100)),
                "thirdConsumeProfit": data.thirdConsumeProfit==null?0:new Number(data.thirdConsumeProfit.toString()).mul(new Number(100)),
                "directRecAgencyProfit": data.directRecAgencyProfit==null?0:new Number(data.directRecAgencyProfit.toString()).mul(new Number(100)),
                "indirectRecAgencyProfit": data.indirectRecAgencyProfit==null?0:new Number(data.indirectRecAgencyProfit.toString()).mul(new Number(100))


            });

        }

    });

    //添加等级
    $(".levelAdd_btn").click(function(){
        add_level = false;

        $("#add_level_form")[0].reset();
        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_level_win")
            ,end:function () {
                table.reload('agency_level_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {

                    }
                });
            }
        });
        form.val('add_level_form', {
            "id":""
        })
    });




    /**
     * oem 新增提交
     */
    form.on('submit(addlevelOk)',function (data) {

        data.field.price =data.field.price*100;
        data.field.consumeProfit =data.field.consumeProfit/100;
        data.field.subsidyProfit =data.field.subsidyProfit/100;

        data.field.directRecAgencyProfit =data.field.directRecAgencyProfit/100;
        data.field.indirectRecAgencyProfit =data.field.indirectRecAgencyProfit/100;

        data.field.directConsumeProfit   =data.field.directConsumeProfit/100;
        data.field.indirectConsumeProfit =data.field.indirectConsumeProfit/100;
        data.field.thirdConsumeProfit    =data.field.thirdConsumeProfit/100;


        if(!add_level){
            add_level = true;
            $.ajax({
                url:"/agency_level/edit",
                data:data.field,
                type:"post",
                success:function (result) {
                    if (result.code == 1){
                        layer.msg("编辑成功",{
                            offset: '32%'
                            ,icon: 1
                            ,time: 0
                            ,btn: '知道了'
                            ,btnAlign:"c"
                            ,shade:0.4
                            ,yes:function () {
                                table.reload('agency_level_table', {
                                });
                                layer.closeAll();
                            }
                        })
                    }else{
                        layer.msg(result.message,{
                            offset: '32%'
                            ,icon: 2
                            ,time: 3000
                        },function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        }else {
            layer.msg("不能重复提交表单",{
                offset: '32%'
                ,icon: 2
                ,time: 3000
            },function () {
            })
        }

        return false;
    });
});

/**
 * 根据服务商配置
 * 渲染
 */
function initLevel() {

    $("#level option").remove();

    var levelNumber = $("#levelNumber").val();

    var profitStrategy = $("#profitStrategy").val();
    if("DEFAULT"==profitStrategy){
            $(".default_profit").show();

            $(".percentage_profit input").removeAttr("required");
            $(".percentage_profit input").removeAttr("lay-verify");

    }else {
            $(".percentage_profit").show();
            $(".default_profit input").removeAttr("required");
            $(".default_profit input").removeAttr("lay-verify");
    }

    for(var i=1;i<=levelNumber;i++){
        var  option = $('<option value="'+i+'" >'+i+'</option>');
        $("#level").append(option);
    }

};

